package hu.xydan;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import java.io.IOException;
import static javax.media.opengl.GL.*;  // GL konstansok
import static javax.media.opengl.GL2.*; // GL2 konstansok
import java.util.ArrayList;
/*
 * @author wuxort
 */
public class OpenGL_init extends GLCanvas implements GLEventListener, KeyListener
{   
   private GLU glu;
   
   Rajzolas rajz = null;
   
   @Override
   public void keyTyped(KeyEvent event) {};
   
   @Override
   public void keyReleased(KeyEvent event) {};
   
   @Override
   public void keyPressed(KeyEvent event) 
   {
       Billentyűzet.init(event);
   };
   
   public OpenGL_init() 
   {
      this.addGLEventListener(this);
      this.addKeyListener(this);
   };
    
   @Override
   public void init(GLAutoDrawable drawable) 
   {
      GL2 gl = drawable.getGL().getGL2();      
      glu = new GLU();                         
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // háttérszín
      gl.glClearDepth(1.0f);      
      gl.glEnable(GL_DEPTH_TEST); 
      gl.glDepthFunc(GL_LEQUAL);  
      gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
      gl.glShadeModel(GL_SMOOTH);  
      
      try 
      {
          ArrayList<Integer> palya = Pályabetöltő.olvas();
          rajz = new Rajzolas(palya);  
      }
      catch (IOException ex) {};
   }

   @Override
   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
   {
      GL2 gl = drawable.getGL().getGL2();  

      if (height == 0) height = 1;   
      float aspect = (float)width / height;

      gl.glViewport(0, 0, width, height);

      gl.glMatrixMode(GL_PROJECTION);  
      gl.glLoadIdentity();             
      glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar
      
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity(); 
   }

   @Override
   public void display(GLAutoDrawable drawable) 
   {
      GL2 gl = drawable.getGL().getGL2(); 
      rajz.rajzol(drawable, gl);
   }

   @Override
   public void dispose(GLAutoDrawable drawable) { }  
}